import 'package:star_author/config/base_configuration.dart';
import 'package:star_author/utils/common_util/date_util.dart';
import 'package:star_author/utils/common_util/log_util.dart';
import 'package:star_author/utils/common_util/shared_preferences/sp_util.dart';
import 'package:star_author/utils/rouces_util.dart';
import 'package:star_author/utils/utils.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class LiveAnnouncement extends StatefulWidget {
  bool isClear;
  LiveAnnouncement({
    Key? key,
    this.isClear: false,
  }) : super(key: key);

  @override
  State<LiveAnnouncement> createState() => _LiveAnnouncementState();
}

class _LiveAnnouncementState extends State<LiveAnnouncement> {
  TextStyle style = TextStyle(
    fontSize: 12.sp,
    color: rgba(255, 255, 255, 1),
    height: 17 / 12,
  );

  bool is_show_content = true;

  String content = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    //内容不变 需要更新本地的公告时间
    content = StreamConf.channelModel!.announcement;
    Map map = SpUtil.getObject(
          StreamConf.channelModel!.id.toString(),
        ) ??
        {};
    is_show_content = map['is_show_content'] ?? true;
    String announcement_at = map['announcement_at'] ?? "";
    //不是第一次进来
    if (announcement_at.isNotEmpty) {
      //如果两个时间不相等 需要设置为true
      if (DateUtil.getDateTime(StreamConf.channelModel!.announcement_at) !=
          DateUtil.getDateTime(announcement_at)) {
        //服务器更新时间大于本地表示服务器内容有更新 需要展示内容
        LogUtil.v("服务器更新时间等于本地时间");
        if (StreamConf.channelModel!.announcement.isNotEmpty) {
          is_show_content = true;
          LogUtil.v("有内容需要展示");
        } else {
          is_show_content = false;
          LogUtil.v("没有内容需要展示");
        }
      } else {
        LogUtil.v("服务器更新时间不等于本地时间");
      }
    } else {
      //本地没有公告时间 获取服务器时间来判断
      if (StreamConf.channelModel!.announcement_at.isNotEmpty &&
          StreamConf.channelModel!.announcement.isNotEmpty) {
        is_show_content = true;
      } else {
        is_show_content = false;
      }

      LogUtil.v("没有公告时间");
    }
    setSpUtil();
    if (mounted) setState(() {});
  }

  setSpUtil() {
    SpUtil.putObject(StreamConf.channelModel!.id.toString(), {
      "is_show_content": is_show_content,
      "announcement_at": StreamConf.channelModel!.announcement_at,
    });
  }

  click() {
    is_show_content = !is_show_content;
    setState(() {});
    setSpUtil();
  }

  @override
  void didUpdateWidget(covariant LiveAnnouncement oldWidget) {
    // TODO: implement didUpdateWidget
    super.didUpdateWidget(oldWidget);
    if (oldWidget.isClear != widget.isClear) {
      if (mounted) setState(() {});
    }
  }

  @override
  Widget build(BuildContext context) {
    double width = (200 / 375) * Get.width;
    double radius = 8.w;
    if (!is_show_content) {
      width = (88 / 375) * Get.width;
      radius = 50.w;
    }
    if (widget.isClear) return Container();
    if (content.isEmpty) return Container();
    return Container(
      decoration: BoxDecoration(
        borderRadius: BorderRadius.circular(radius),
        color: rgba(0, 0, 0, 0.40),
      ),
      padding: EdgeInsets.all(8.w),
      width: width,
      child: Column(
        children: [
          GestureDetector(
            child: Row(
              mainAxisAlignment: MainAxisAlignment.spaceBetween,
              children: [
                Row(
                  children: [
                    RoucesUtil.getAssetsIcon('ic_live_announcement_lt', 12),
                    SizedBox(
                      width: 3.w,
                    ),
                    Text(
                      "公告",
                      style: style,
                    ),
                  ],
                ),
                GestureDetector(
                  child: RotatedBox(
                    quarterTurns: is_show_content ? -1 : 1,
                    child: Image(
                      image: AssetImage('images/lt_common/right_arrow.png'),
                      width: 7.w,
                    ),
                  ),
                  onTap: () {
                    click();
                  },
                  behavior: HitTestBehavior.opaque,
                ),
              ],
            ),
            onTap: () {
              click();
            },
            behavior: HitTestBehavior.opaque,
          ),
          if (is_show_content && content.isNotEmpty)
            Container(
              child: Text(
                content,
                style: style,
              ),
              margin: EdgeInsets.only(
                top: 10.w,
              ),
            )
        ],
        crossAxisAlignment: CrossAxisAlignment.start,
        mainAxisAlignment: MainAxisAlignment.start,
      ),
    );
  }
}
